package com.ljw.test.util;


import android.util.Log;

import com.ljw.test.MyApplication;
import com.ljw.test.dao.ChildDao;
import com.ljw.test.dao.ParentDao;
import com.ljw.test.model.Child;
import com.ljw.test.model.Parent;

import java.util.Arrays;
import java.util.List;

public class GreenDaoManager {
    private static final String TAG = "GreenDaoManager";

    private static GreenDaoManager manager = new GreenDaoManager();

    private final ParentDao parentDao;

    private final ChildDao childDao;

    public static GreenDaoManager getInstance() {
        return manager;
    }

    private GreenDaoManager() {
        parentDao = MyApplication.getApplication().getDaoSession().getParentDao();
        childDao = MyApplication.getApplication().getDaoSession().getChildDao();
    }

    public Parent queryChildren(final String parent) {
        Parent unique = null;
        try {
            unique = parentDao.queryBuilder().where(ParentDao.Properties.Parent.eq(parent)).build().unique();
//             final Query query = parentDao.queryBuilder().where(ParentDao.Properties.Parent.eq(parent)).build();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Parent p = parentDao.queryBuilder().where(ParentDao.Properties.Parent.eq(parent)).build().forCurrentThread().list().get(0);
                    System.out.println("--->" + Arrays.toString(p.getChildList().toArray()));
                }
            });

        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return unique;
    }

    /**
     * 插入parent数据
     *
     * @param bean
     * @return
     */
    public long insertParentData(Parent bean) {
        Log.d(TAG, "insert parent===>" + bean);
        return parentDao.insertOrReplace(bean);
    }


    /**
     * 删除parent数据
     *
     * @param id
     */
    public void deleteParentData(Long id) {
        Log.d(TAG, "delete　parent===>id" + id);
        parentDao.deleteByKey(id);
    }

    /**
     * 更新parent数据
     *
     * @param bean
     */
    public void updateParentData(Parent bean) {
        Log.d(TAG, "update parent===>" + bean);
        parentDao.update(bean);
    }


    /**
     * 查询所有的parent数据
     *
     * @return
     */
    public List<Parent> queryAllParentData() {
        List<Parent> list = parentDao.queryBuilder().list();

        Log.i(TAG, "queryAllParentData: list::" + list);

        return list;
    }


    /**
     * 查询所有parent下对应的children
     */
    public void queryAllChildrenForParent() {
        List<Parent> list = parentDao.queryBuilder().list();

        for (Parent bean : list) {
            List<Child> childrenBeanList = bean.getChildList();
            Log.i(TAG, "queryChildrenForParentId: childrenBeanList:::" + childrenBeanList);
        }

    }


    /**
     * 插入children数据
     *
     * @param bean
     * @return
     */
    public long insertChildrenData(Child bean) {
        Log.d(TAG, "insert children===>" + bean);
        cleanCache();
        return childDao.insert(bean);
    }

    /**
     * 删除children数据
     *
     * @param id
     */
    public void deleteChildrenData(Long id) {
        Log.d(TAG, "delete　children===>id" + id);
        childDao.deleteByKey(id);
    }

    /**
     * 更新children数据
     *
     * @param bean
     */
    public boolean updateChildrenData(Child bean) {
        try {
            Log.d(TAG, "update children===>" + bean);
            childDao.update(bean);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean queryHasChild(Child bean) {
        return childDao.hasKey(bean);
    }

    /**
     * 查询所有的children数据
     *
     * @return
     */
    public List<Child> queryAllChildrenData() {
        //两种方式获取是一样的，查看源码便知
        List<Child> list = childDao.queryBuilder().list();
        List<Child> list1 = childDao.loadAll();

        Log.i(TAG, "queryAllChildrenData: list::" + list + " list1::" + list1);
        return list;
    }

    /**
     * 查询某个parentId下对应的children
     *
     * @param parentId
     * @return
     */
    public List<Child> queryChildrenDataAccordField(Long parentId) {
        List<Child> list = childDao.queryBuilder().where(ChildDao.Properties.ParentId.eq(parentId)).list();
        Log.i(TAG, "queryChildrenDataAccordField: list::" + list);
        return list;
    }

    public void cleanCache() {
        parentDao.detachAll();
    }

}
